package com.bxd.baodan.activity;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.bxd.baodan.App;
import com.bxd.baodan.R;
import com.bxd.baodan.adapter.CommonAdapter;
import com.bxd.baodan.adapter.ViewHolder;
import com.bxd.baodan.app.Constant;
import com.bxd.baodan.app.JsonApi;
import com.bxd.baodan.app.LoginInfo;
import com.bxd.baodan.item.ShellBean;
import com.bxd.baodan.util.GlideUtil;
import com.bxd.baodan.util.GsonUtil;
import com.bxd.baodan.util.NetUtil;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by lizhichao on 18/2/3.
 */

public class ShellActivity extends BaseActivity {


    private CommonAdapter<ShellBean.NoviceBean> noviceAdapter;
    List<ShellBean.NoviceBean> list = new ArrayList<>();

    private Set<Integer> select = new HashSet<>();


    int noviceCount;


    private ImageView avatar;
    private TextView name;
    private TextView shellCount;
    private TextView shopping;
    private ListView lv;
    private ImageView iv;
    private LinearLayout shell_detail_ly;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shell);
        setActionBarTitle("我的贝壳");

        initView();
        setAdapter();
       // getData();
    }

    private void initView() {
        iv = findViewById(R.id.iv_bar_right);
        iv.setImageResource(R.drawable.wenhao);
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO: 18/2/5 积分说明
                String url = Constant.base_url + "/html/integral/shuoming";
                openActivity(WebViewPager.class, new String[]{"startUrl"}, new String[]{url});
            }
        });
        avatar = (ImageView) findViewById(R.id.avatar);
        name = (TextView) findViewById(R.id.name);
        shell_detail_ly = (LinearLayout) findViewById(R.id.shell_detail_ly);
        shellCount = (TextView) findViewById(R.id.shell_count);
        shopping = (TextView) findViewById(R.id.shopping);
        lv = (ListView) findViewById(R.id.lv);

        shopping.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String url = Constant.base_url + "/html/integral/mall?appid=wenwen";
                openActivity(WebViewPager.class, new String[]{"startUrl"}, new String[]{url});
            }
        });
        shell_detail_ly.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (loginStatus()) {

                    String url = Constant.base_url + "/html/integral/activity?uid=" + LoginInfo.getInstance().getUid();
                    openActivity(WebViewPager.class, new String[]{"startUrl"}, new String[]{url});
                }
            }
        });


    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {

        NetUtil netUtil = new NetUtil(this, JsonApi.INTEGRAL_MYTASK);
        netUtil.postRequest("", new NetUtil.DataListener() {
            @Override
            public void onComplete(String data) {
                ShellBean shellBean = GsonUtil.fromJson(data, ShellBean.class);
                list.clear();

                for (ShellBean.NoviceBean bean :
                        shellBean.daily) {
                    bean.type = "daily";
                }
                for (ShellBean.NoviceBean bean :
                        shellBean.novice) {
                    bean.type = "novice";
                }
                list.addAll(shellBean.novice);

                noviceCount = shellBean.novice.size();
                list.addAll(shellBean.daily);

                select.add(1);
                noviceAdapter.notifyDataSetChanged();

                GlideUtil.loadCircle(ShellActivity.this, shellBean.avatar, avatar);
                name.setText(shellBean.nickname);
                shellCount.setText(shellBean.conch);


            }
        });

    }

    private void setAdapter() {

        noviceAdapter = new CommonAdapter<ShellBean.NoviceBean>(this, list, R.layout.shell_item) {
            @Override
            public void convert(final ViewHolder holder, final ShellBean.NoviceBean item) {
                final View isCompleted_ly = holder.getView(R.id.isCompleted_ly);

                final View arrow_down = holder.getView(R.id.arrow_down);
                final View arrow_up = holder.getView(R.id.arrow_up);
                arrow_down.setVisibility(View.VISIBLE);
                arrow_up.setVisibility(View.GONE);


                //加头
                if (holder.getPosition() == 0) {
                    holder.getView(R.id.head).setVisibility(View.VISIBLE);
                    if (0 == noviceCount) {
                        ((TextView) holder.getView(R.id.type)).setText("每日任务");
                    } else {
                        ((TextView) holder.getView(R.id.type)).setText("新手任务");
                    }
                } else if (holder.getPosition() == noviceCount) {
                    holder.getView(R.id.head).setVisibility(View.VISIBLE);
                    ((TextView) holder.getView(R.id.type)).setText("每日任务");
                } else {
                    holder.getView(R.id.head).setVisibility(View.GONE);
                }

                holder.setText(R.id.cname, item.ucr_cname);
                holder.setText(R.id.conchNum, "+" + item.ucr_conchNum + "贝壳");
                holder.setText(R.id.note, item.ucr_note);


                if (item.isCompleted.equals("1")) {
                    holder.getView(R.id.Completed).setVisibility(View.VISIBLE);
                    holder.getView(R.id.noCompleted).setVisibility(View.GONE);

                } else {
                    holder.getView(R.id.Completed).setVisibility(View.GONE);
                    holder.getView(R.id.noCompleted).setVisibility(View.VISIBLE);
                }
                isCompleted_ly.setVisibility(View.GONE);

                for (int i = 0; i < select.size(); i++) {
                    if (select.contains(holder.getPosition())) {
                        isCompleted_ly.setVisibility(View.VISIBLE);
                        arrow_down.setVisibility(View.GONE);
                        arrow_up.setVisibility(View.VISIBLE);
                    } else {
                        arrow_down.setVisibility(View.VISIBLE);
                        arrow_up.setVisibility(View.GONE);
                    }
                }

                holder.getView(R.id.content_ry).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (select.contains(holder.getPosition())) {
                            isCompleted_ly.setVisibility(View.GONE);
                            arrow_down.setVisibility(View.VISIBLE);
                            arrow_up.setVisibility(View.GONE);
                            select.remove(holder.getPosition());
                        } else {
                            arrow_down.setVisibility(View.GONE);
                            arrow_up.setVisibility(View.VISIBLE);
                            select.add(holder.getPosition());
                            isCompleted_ly.setVisibility(View.VISIBLE);
                            //      ObjectAnimator transAnim = ObjectAnimator.ofFloat(isCompleted_ly, "translationY", -isCompleted_ly.getHeight(),0 );
                            ObjectAnimator alphaAnim = ObjectAnimator.ofFloat(isCompleted_ly, "alpha", 0, 1);
                            AnimatorSet animatorSet = new AnimatorSet();
                            // animatorSet.play(transAnim).with(alphaAnim);
                            animatorSet.play(alphaAnim);
                            animatorSet.setDuration(500);
                            animatorSet.setInterpolator(new LinearInterpolator());
                            animatorSet.start();
                        }


                    }
                });


                holder.getView(R.id.noCompleted).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        gotoPage(item.ucr_ename);


                    }
                });

            }
        };


        lv.setAdapter(noviceAdapter);

    }

    private void gotoPage(String str) {

        switch (str) {
            //注册成功
            case "RegisteredSuccessfully":


                break;
//完善个人信息
            case "PerfectPersonalInformation":
                openActivity(UserSettingActivity.class);

                break;
//首次阅读
            case "FirstReading":
                openActivity(GanHuoActivity.class);
                break;
//首次提问
            case "FirstAskQuestions":
                openActivity(StartAskActivity.class);
                break;


            //首次点赞
            case "FirstThumbUp":
                // TODO: 18/2/6 需要跳转
                sendBroadcast(new Intent(Constant.ACTION_MAIN_BAOWWEN));
                finish();
                //openActivity(MainActivity.class);

                break;
//首次回复
            case "FirstComment":
                // TODO: 18/2/6 需要跳转
                sendBroadcast(new Intent(Constant.ACTION_MAIN_BAOWWEN));
                finish();
                //openActivity(MainActivity.class);

                break;
//首次设置最佳回答
            case "TirstSetTheBestAnswer":

                openActivity(MyQuestionActivity.class);
                break;
//上传保单
            case "UploadThePolicy":
                openActivity(MainActivity.class);
                break;


            //家庭智能评测
            case "FamilyIntelligenceEvaluation":
                openActivity(FamilyDesignActivity.class);
                break;


//每日登录
            case "TheDailyLogin":

                break;
//问答提问
            case "AskQuestion":
                openActivity(StartAskActivity.class);
                break;
//回复评论
            case "ReplyToComment":
                sendBroadcast(new Intent(Constant.ACTION_MAIN_BAOWWEN));
                finish();
                //openActivity(MainActivity.class);
                break;

//设置最佳回答
            case "SetTheBestAnswer":
                openActivity(MyQuestionActivity.class);
                break;
//邀请好友
            case "InviteFriends":
                if (loginStatus()) {
                    MobclickAgent.onEvent(App.getContext(), "v6_1_3_WD_yqhy");
                    String url1 = Constant.root_url + "html/invitejoin/invite" + "?uid=" + LoginInfo.getInstance().getUser().getU_id();
                    openActivity(WebViewPager.class, new String[]{"startUrl"}, new String[]{url1});
                }
                break;
//分享文章
            case "ShareTheArticle":
                openActivity(GanHuoActivity.class);
                break;

        }
    }
}
